<?php 
	include "include.php";

	$user = SecurityManager::getUser();
	$service = new UserService();
		
	if( $user == null ) {
		$user = new UserModel();
	} else {
		$user = $service->findById(array($user->getId()));
	}
?>
<div id="cadastro">
	<h3>Cadastro de Usu&aacute;rio</h3>
	<form id="formCadastro" method="POST" action="?option=usersave" onsubmit="return validate();">
		<input type="hidden" name="id" value="<?php echo $user->getId(); ?>">
		<table width="460px" cellspacing="0">
			<tr>
				<td class="label">Nome</td>
				<td class="input"><input type="text" class="text" id="name" name="name" tabindex="1" value="<?php echo $user->getName(); ?>"></td>
			</tr>
			<tr>
				<td class="label">Usu&aacute;rio</td>
				<td class="input"><input type="text" class="text" id="login" name="login" tabindex="3" value="<?php echo $user->getLogin(); ?>"></td>
			</tr>
			<tr>
				<td class="label">Senha</td>
				<td class="input"><input type="password" class="text" id="password" name="password" tabindex="4" value="<?php echo $user->getPassword(); ?>" ></td>
			</tr>
			<tr>
				<td class="label">Confirma&ccedil;&atilde;o da Senha</td>
				<td class="input"><input type="password" class="text" id="password2" name="password2" tabindex="5" value=""></td>
			</tr>
			<tr>
				<td class="label">E-mail GMAIL</td>
				<td class="input"><input type="text" class="text" id="gmailuser" name="gmailuser" tabindex="6" value="<?php echo $user->getGmailuser(); ?>" ></td>
			</tr>
			<tr>
				<td class="label">Senha GMAIL</td>
				<td class="input"><input type="password" class="text" id="gmailpassword" name="gmailpassword" tabindex="7" value="<?php echo $user->getGmailpassword(); ?>" ></td>
			</tr>
			<tr>
				<td class="label">Deseja receber alertas por SMS?</td>
				<td class="input">
					<select id="isAlertActive" name="isAlertActive" onchange="if(this.value == 1) cc.marcio.dom.enable('alertFor'); else cc.marcio.dom.disable('alertFor');">
						<option value="" <?php if(ord($user->getIsAlertActive()) != 0 && ord($user->getIsAlertActive()) != 1) echo "selected"; ?>>Selecione</option>
						<option value="0" <?php if(ord($user->getIsAlertActive()) == 0) echo "selected"; ?>>N&atilde;o</option>
						<option value="1" <?php if(ord($user->getIsAlertActive()) == 1) echo "selected"; ?>>Sim</option>
					</select>
					<select id="alertFor" name="alertFor" <?php if($user->getAlertFor() < 1) echo "disabled"; ?>>
						<option value="" <?php if($user->getAlertFor() != 1 && ord($user->getAlertFor()) != 2 && ord($user->getAlertFor()) != 3 && ord($user->getAlertFor()) != 4) echo "selected"; ?>>SMS para eventos em</option>
						<option value="1" <?php if($user->getAlertFor() == 1) echo "selected"; ?>>Meu Planeta</option>
						<option value="2" <?php if($user->getAlertFor() == 2) echo "selected"; ?>>Meu Quadrante</option>
						<option value="3" <?php if($user->getAlertFor() == 3) echo "selected"; ?>>Meu Setor</option>
						<option value="4" <?php if($user->getAlertFor() == 4) echo "selected"; ?>>Todos Planetas</option>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" class="input">
					<button type="reset">Reset</button>&nbsp;<button type="submit">Enviar</button>
				</td>
			</tr>
		</table>	
	</form>	
</div>
<script language="javascript">
	function validate() {
		if( cc.marcio.functions.formValidate(['name', 'login', 'password', 'password2', 'gmailuser', 'gmailpassword'],['Nome', 'Usuario', 'Senha', 'Confirmacao da Senha', 'E-mail GMAIL', 'Senha GMAIL'], 'formCadastro') ) {
			if( obj('isAlertActive').value == 1 && obj('alertFor').value == "" ) {
				alert('Selecione uma opcao para envio de SMS');
				return false;
			}
			return true;
		}	
		return false;
	}

	function obj(id) {
		return document.getElementById(id);
	}
</script>